<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        required
        name="mobile"
        label="手机号"
        placeholder="请填写11位手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '请填写正确的手机号' },
        ]"
      />
      <van-field
        v-model="user.code"
        required
        type="password"
        name="code"
        label="密码"
        placeholder="请填写6位密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\d{6}$/, message: '请填写正确的密码' },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isLoading"
          loading-text="加载中..."
          :disabled="isLoading"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
// import { Notify } from 'vant'
import Notify from '@/ui/Notify'
import { setToken } from '@/utils/token.js'
import { setStorage } from '@/utils/storage'
export default {
  name: 'Login',
  // 在style上加了scoped后，如果你想修改替换组件标签的根标签的样式，直接修改就行，如果你想要修改替换标签的根标签里面的标签的样式，就需要在选择器前面加/deep/，style上加lang="less"表名自己里面写的是less
  data () {
    return {
      user: {
        mobile: '18739135679',
        code: '246810'
      },
      isLoading: false
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      this.isLoading = true
      try {
        const res = await loginAPI(values)
        setToken(res.data.data.token)
        setStorage('refresh_token', res.data.data.refresh_token)
        Notify({ type: 'success', message: '登录成功' })
        this.isLoading = false
        this.$router.replace({
          path: this.$route.query.path || '/layout/home'
        })
      } catch (err) {
        // console.dir(err)
        Notify({ type: 'danger', message: err.response.data.message })
        this.isLoading = false
      }
    }
  }
}
</script>

<style scoped >
</style>
